package com.huawei.esdk.tp.device.smc;

import org.apache.log4j.Logger;

import com.huawei.esdk.platform.authorize.itf.IAuthorize;
import com.huawei.esdk.platform.authorize.itf.IAuthorizePolicy;
import com.huawei.esdk.platform.common.ThreadLocalHolder;
import com.huawei.esdk.platform.common.config.ConfigManager;
import com.huawei.esdk.platform.common.constants.ESDKConstant;
import com.huawei.esdk.platform.common.utils.ApplicationContextUtil;
import com.huawei.esdk.platform.common.utils.StringUtils;
import com.huawei.esdk.platform.commu.itf.ICXFSOAPProtocolAdapter;
import com.huawei.esdk.platform.commu.itf.IProtocolAdapterManager;
import com.huawei.esdk.platform.nemgr.base.MultiConnDeviceBase;
import com.huawei.esdk.platform.nemgr.itf.IDeviceConnection;
import com.huawei.esdk.platform.nemgr.itf.IDeviceManager;
import com.huawei.esdk.tp.common.constants.InfoKeys;
import com.huawei.esdk.tp.southcommu.TPCXFSOAPCustProvider;

public class SMCDevice extends MultiConnDeviceBase
{
    private static final Logger LOGGER = Logger
            .getLogger(MultiConnDeviceBase.class);

    private IDeviceManager deviceManager = ApplicationContextUtil
            .getBean("deviceManager");

    protected IProtocolAdapterManager protocolAdapterManager = ApplicationContextUtil
            .getBean("protocolAdapterManager");

    protected IAuthorize authorize = ApplicationContextUtil.getBean("authorize");
    
    /*
     * Service Access Point (URL)
     */
    private String sap;

    protected ICXFSOAPProtocolAdapter cxfSOAPProtocolAdapter;

    protected String getSap()
    {
        return sap;
    }

    protected void setSap(String sap)
    {
        this.sap = sap;
    }

    public SMCDevice(String sap)
    {
        LOGGER.debug("sap = " + sap);
        this.sap = sap;
        this.cxfSOAPProtocolAdapter = (ICXFSOAPProtocolAdapter) protocolAdapterManager
                .getProtocolInstanceByType(ESDKConstant.PROTOCOL_ADAPTER_TYPE_SOAP_CXF, sap);

        cxfSOAPProtocolAdapter
                .setCXFSOAPCustProvider(new TPCXFSOAPCustProvider());

        prepareDeviceCapability();
    }

    protected void prepareDeviceCapability()
    {
        addServiceObjectMap(ISiteManagerCapability.class,
                new SiteManagerCapabilityImpl(this.cxfSOAPProtocolAdapter));
        addServiceObjectMap(
                IConferenceControlCapability.class,
                new ConferenceControlCapabilityImpl(this.cxfSOAPProtocolAdapter));
        addServiceObjectMap(
                IConferenceManagerCapability.class,
                new ConferenceManagerCapabilityImpl(this.cxfSOAPProtocolAdapter));
    }

    @Override
    public IDeviceConnection createConnection(String appId, String sap,
            String user, String pwd)
    {       
        return new SMCConnection(cxfSOAPProtocolAdapter, this, user, pwd, buildAuthorizePolicy());
    }
    
    protected IAuthorizePolicy buildAuthorizePolicy()
    {
        String product = (String) ThreadLocalHolder.get().getEntities().get(ESDKConstant.CURRENT_PRODUCT);
        if (StringUtils.isEmpty(product))
        {
            product = "tp";
        }
        String authPolicy = ConfigManager.getInstance().getValue(product + ".authentication.policy");       
        return authorize.getAuthPolicyImpl(authPolicy, deviceId);
    }

    @Override
    public String getConnIdFromContext()
    {
        return (String) ThreadLocalHolder.get().getEntities()
                .get(InfoKeys.SDK_SESSION_ID.toString());
    }

    @Override
    public Boolean releaseConns()
    {
        for (String key : id2Connection.keySet())
        {
            IDeviceConnection conn = id2Connection.get(key);
            deviceManager.releaseConn(conn);
        }
        return true;
    }

}